﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

public partial class admin_updateCourse : System.Web.UI.Page
{
    db_course course = new db_course();
    static DataSet ds = new DataSet();
    string teacherId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Server.Execute("checkTeach.aspx");  // 此处用户权限验证
        teacherId = Session["teacherID"].ToString();

        if (Request.QueryString["id"] == null)
        {
            Response.Redirect("courseInfo.aspx");
        }

        if (!Page.IsPostBack)
        {
            string courseId = Request.QueryString["id"];
            lb_Id.Text = courseId;
            initCourseType();
            ds = course.selectCourseONE(courseId);
            if (!teacherId.Equals(ds.Tables[0].Rows[0]["teacherId"].ToString()))
            {
                Response.Write("<script language=javascript>alert('这个不是你的课程.无法进行操作.');location.href='courseInfo.aspx';</script>");
                Response.End();
                return;
            }
            bind_Course(ds);
        }
    }
    public void bind_Course(DataSet ds)
    {
        if (ds.Tables.Count != 0)
        {
            ddl_courseType.SelectedValue = ds.Tables[0].Rows[0]["courseType"].ToString();
            tb_about.Text = ds.Tables[0].Rows[0]["about"].ToString();
            tb_courseHour.Text = ds.Tables[0].Rows[0]["courseHour"].ToString();
            tb_gold.Text = ds.Tables[0].Rows[0]["gold"].ToString();
            tb_courseName.Text = ds.Tables[0].Rows[0]["courseName"].ToString();
        }
    }

    private void initCourseType()
    {
        DataSet ds = new DataSet();
        ds = course.getCourseType();
        if (ds.Tables.Count != 0)
        {
            ddl_courseType.DataSource = ds.Tables[0].DefaultView;
            ddl_courseType.DataTextField = "typeName";
            ddl_courseType.DataValueField = "typeId";
            ddl_courseType.DataBind();
            ddl_courseType.Items.Insert(0, new ListItem("请选择课程类型", ""));
            ddl_courseType.Items[0].Selected = true;
        }
    }

    protected void btn_ok_Click(object sender, EventArgs e)
    {
        string courseID = Request.QueryString["id"];
        string courseType = ddl_courseType.SelectedValue;
        string about = tb_about.Text;
        string courseHour = tb_courseHour.Text;
        
        string gold = tb_gold.Text;
        string courseName = tb_courseName.Text;
        try
        {
            float.Parse(courseHour);
        }
        catch
        {
            Response.Write("<script language=javascript>alert('输入正确的课时！');</script>");
            tb_courseHour.Focus();
            return;
        }
        try
        {
            float.Parse(gold);
        }
        catch
        {
            Response.Write("<script language=javascript>alert('输入正确的售价！');</script>");
            tb_gold.Focus();
            return;
        }

        if (course.updateCourse(courseID, courseType, about, courseHour, teacherId, gold, courseName))
        {
            Response.Write("<script language=javascript>alert('更新课程信息成功！');location.href='courseInfo.aspx';</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('失败！');</script>");
        }
    }

    protected void btn_res_Click(object sender, EventArgs e)
    {
        bind_Course(ds);
    }
}